package com.qwe.graduation.controller.core;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qwe.graduation.entity.User;
import com.qwe.graduation.service.IUserService;
import com.qwe.graduation.utils.RedisUtil;
import com.qwe.graduation.utils.Result;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @author lzf
 * @date 2022/10/21 11:13
 * @description AuthCodeController
 */
@RestController
@RequestMapping("/")
public class AuthCodeController {
    @Autowired
    RedisUtil  redisUtil;

    @Autowired
    private IUserService IUserService;

    /**
     * 登录验证尝试
     * @param
     * @throws Exception
     */
    @GetMapping( "/auth/code")
    public Result<String> getToken(@RequestParam String username,
                                  @RequestParam(required = false) String password,
                                  @RequestParam(required = false) String code,
                                  @RequestParam(required = false) String codeId){
        if (!check(codeId, code)){
            System.out.println(check(code, codeId));
            return Result.error("验证码错误");
        }
        User one = IUserService.getOne(new LambdaQueryWrapper<User>().eq(User::getName, username));
        if(one == null){
            return Result.error("用户不存在");
        }
        if (one.getPassword().equals(password)== false){
            return Result.error("密码错误");
        }
        String token = UUID.randomUUID().toString();
        redisUtil.set(token,one);
        User user = (User) redisUtil.get(token);
        System.out.println(user);
        return Result.OK("用户登录了",token);
    }

    /**
     * 图形验证码
     * @param request
     * @param response
     * @throws Exception
     */
    @GetMapping("/captcha")
    public String captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        specCaptcha.setFont(Captcha.FONT_1);
        String id = UUID.randomUUID().toString();
        response.setHeader("id", id);
        CaptchaUtil.out(specCaptcha, request, response);
        String verCode = specCaptcha.text().toLowerCase();
        //不设置过期
        //redisUtil.set(id, verCode);
        //设置一分钟过期
        redisUtil.set(id,verCode,8000);
        System.out.println(id);
        return id;
    }

    /**
     * 校验图形验证码
     * @param
     * @return
     */
    @PostMapping(value = "/check")
    public boolean check(String id,String code) {
//        JSONObject jsonObject = JSON.parseObject(info);
        //获取传过来的id 和 code
//        String id = jsonObject.getString("id");
//        String code = jsonObject.getString("code");
        String s = "";
        //获取redis里面存的code
        Object obj = redisUtil.get(id);
        if(obj != null){
            s = obj.toString();
        }
        //比较输入的code和redis的code
        boolean flag = code.equalsIgnoreCase(s);
        //匹配成功就删除redis存储
        if(flag){
            redisUtil.del(id);
            System.out.println("验证码成功");
        }
        System.out.println("验证码错误");
        return flag;
    }
}
